package com.seele.tigerwallet.ui.utils;

import android.content.Context;
import android.content.Intent;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;
import android.util.Log;


public class FingerprintUtils {
    private static final String ACTION_SETTING = "android.settings.SETTINGS";
    private FingerprintManagerCompat mFingerprintManagerCompat;
    private CancellationSignal mCancellationSignal = null;

    private OnFingerprintListener mListener;

    public void setFingerprintListener(OnFingerprintListener listener) {
        this.mListener = listener;

    }

    public void clearFingerprintListener() {
        if (null != mListener) {

            this.mListener = null;
        }
    }

    public interface OnFingerprintListener {

        void onHardwareDetected(boolean hardwareDetected);//不支持指纹验证

        void onEnrolledFingerprints(boolean b);//没有录入过指纹

        //多次指纹密码验证错误后，进入此方法；并且，不可再验（短时间）
        //errorCode是失败的次数
        void onAuthenticationError(int errorCode, CharSequence errString);


        //指纹验证失败，可再验，可能手指过脏，或者移动过快等原因。
        void onAuthenticationHelp(int helpCode, CharSequence helpString);

        //指纹密码验证成功
        void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result);


        //指纹验证失败，指纹识别失败，可再验，错误原因为：该指纹不是系统录入的指纹。
        void onAuthenticationFailed();


    }

    public FingerprintUtils init(Context context) {
        if (null == mFingerprintManagerCompat) {
            mFingerprintManagerCompat = FingerprintManagerCompat.from(context);
        }
        if (null == mCancellationSignal) {
            mCancellationSignal = new CancellationSignal();
        }
        return this;
    }

    public void isHardwareDetected() {
        //判断是否支持指纹验证
        if (null != mListener) {
            mListener.onHardwareDetected(mFingerprintManagerCompat.isHardwareDetected());

        }
        //是否有指纹库
        if (null != mListener) {
            mListener.onEnrolledFingerprints(mFingerprintManagerCompat.hasEnrolledFingerprints());
        }
    }

    public boolean hasEnrolledFingerprints() {
        return mFingerprintManagerCompat.hasEnrolledFingerprints();
    }


    //跳转安全界面
    public static void openFingerPrintSettingPage(Context context) {
        Intent intent = new Intent(ACTION_SETTING);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (Exception e) {
        }
    }


    public void handlerFingerprint() {
        if (null == mCancellationSignal) {
            mCancellationSignal = new CancellationSignal();
        }

        Log.e("authenticate", "::handlerFingerprint:");
        mFingerprintManagerCompat.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                super.onAuthenticationError(errMsgId, errString);
                //多次指纹密码验证错误后，进入此方法；并且，不可再验（短时间）
                //errMsgId是失败的次数
                if (null != mListener) {
                    mListener.onAuthenticationError(errMsgId, errString);
                }

                Log.e("authenticate", "::onAuthenticationError:" + errMsgId + "::errString:" + errString);
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                super.onAuthenticationHelp(helpMsgId, helpString);
                //指纹验证失败，可再验，可能手指过脏，或者移动过快等原因。
                Log.e("authenticate", "::onAuthenticationHelp:" + helpMsgId + "::helpString:" + helpString);

                if (null != mListener) {
                    mListener.onAuthenticationHelp(helpMsgId, helpString);
                }
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                //指纹密码验证成功
                if (null != mListener) {
                    mListener.onAuthenticationSucceeded(result);
                }

                Log.e("authenticate", "::onAuthenticationSucceeded:" + "AuthenticationResult:" + result);
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                //指纹验证失败，指纹识别失败，可再验，错误原因为：该指纹不是系统录入的指纹。
                Log.e("authenticate", "::onAuthenticationFailed:");

                if (null != mListener) {
                    mListener.onAuthenticationFailed();
                }
            }
        }, null);

    }

    public void cancel() {
        if (!mCancellationSignal.isCanceled()) {
            mCancellationSignal.cancel();
        }
        mCancellationSignal = null;
        mFingerprintManagerCompat = null;
    }
}
